package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.specification.SpecVO;
import com.dongdongshop.pojo.specification_option.TbSpecificationOption;
import com.dongdongshop.pojo.type_template.TbTypeTemplate;
import com.dongdongshop.service.specification_option.SpecificationOptionService;
import com.dongdongshop.service.type_template.TypeTemPlateService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("specification")
public class SpecificationController {
    @Reference
    private TypeTemPlateService typeTemPlateService;
    @Reference
    private SpecificationOptionService specificationOptionService;

    @RequestMapping("getSpecAndOptionsByTemplateId")
    @ResponseBody
    private Result getSpecAndOptionsByTemplateId(Long id){
        TbTypeTemplate typeTemplate = typeTemPlateService.getTypeTemplate(id);
        List<SpecVO> specVOList = JSON.parseArray(typeTemplate.getSpecIds(),SpecVO.class);
        for (SpecVO specVO : specVOList) {
            List<TbSpecificationOption> optionList = specificationOptionService.getSpecificationOption(specVO.getId());
            specVO.setOptions(optionList);
        }
        return Result.ok().setData(specVOList);
    }

}
